﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Criterion;

namespace SmartProject.Entity
{
    public enum SprintType
    {
        All,
        Completed,
        Pending,
        Started
    }

    public class Sprint : EntityBase
    {
        public Sprint()
        {
            Tasks = new List<Task>();
        }

        public virtual string Description { get; set; }

        public virtual DateTime? Started { get; set; }

        public virtual DateTime? Ended { get; set; }

        public virtual DateTime Scheduled { get; set; }

        public virtual int EstimatedDays { get; set; }

        /// <summary>
        /// Gets or sets the owner of this Sprint Cycle. The owner is someone who manages
        /// the Sprint Cycle.
        /// </summary>
        public virtual User Owner { get; set; }

        public virtual IList<Task> Tasks { get; set; }

        public static IList<Sprint> GetSprints(ISession session, SprintType sprintType)
        {
            IList<ICriterion> expressions = new List<ICriterion>();
            switch (sprintType)
            {
                case SprintType.All:
                    // Do nothing
                    break;

                case SprintType.Pending:
                    expressions.Add(Restrictions.IsNull("Started"));
                    break;

                case SprintType.Completed:
                    expressions.Add(Restrictions.IsNotNull("Ended"));
                    break;

                case SprintType.Started:
                    expressions.Add(Restrictions.IsNotNull("Started"));
                    expressions.Add(Restrictions.IsNull("Ended"));
                    break;
            }
            IList<Order> orderings = new List<Order>();
            orderings.Add(Order.Desc("Created"));
            IList<EntityBase> entities = Find(session, null, orderings, typeof(Sprint));
            IList<Sprint> sprints = new List<Sprint>();
            for (int i = 0; i < entities.Count; i++)
            {
                sprints.Add((Sprint)entities[i]);
            }
            return sprints;
        }

        /// <summary>
        /// Gets a sprint instance by sprint id.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="sprintId"></param>
        /// <returns></returns>
        public static Sprint GetSprint(ISession session, int sprintId)
        {
            return (Sprint)EntityBase.GetEntity(session, sprintId, typeof(Sprint));
        }

        /// <summary>
        /// Deletes the inactive Sprint instances.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        public static void Clean(ISession session)
        {
            Delete(session, typeof(Sprint));
        }
    }
}
